package com.weilan.ark3.utils;


import com.weilan.ark3.common.Communication;
import com.weilan.ark3.common.GlobalParam;

public class ComFault {
    public static short FaultNum() {
        int decimalNumber = 0;
        if (GlobalParam.com1Fault == 1 && GlobalParam.com3Fault == 1 && GlobalParam.com2Fault == 1) {
            decimalNumber = Integer.parseInt("0000000000000111", 2);
        } else if (GlobalParam.com1Fault == 1 && GlobalParam.com2Fault == 1 && GlobalParam.com3Fault == 0) {
            decimalNumber = Integer.parseInt("0000000000000011", 2);
        } else if (GlobalParam.com1Fault == 1 && GlobalParam.com2Fault == 0 && GlobalParam.com3Fault == 1) {
            decimalNumber = Integer.parseInt("0000000000000101", 2);
        } else if (GlobalParam.com1Fault == 0 && GlobalParam.com2Fault == 1 && GlobalParam.com3Fault == 1) {
            decimalNumber = Integer.parseInt("0000000000000110", 2);
        } else if (GlobalParam.com1Fault == 1 && GlobalParam.com2Fault == 0 && GlobalParam.com3Fault == 0) {
            decimalNumber = Integer.parseInt("0000000000000001", 2);
        } else if (GlobalParam.com1Fault == 0 && GlobalParam.com2Fault == 1 && GlobalParam.com3Fault == 0) {
            decimalNumber = Integer.parseInt("0000000000000010", 2);
        } else if (GlobalParam.com1Fault == 0 && GlobalParam.com2Fault == 0 && GlobalParam.com3Fault == 1) {
            decimalNumber = Integer.parseInt("0000000000000100", 2);
        } else if (GlobalParam.com1Fault == 0 && GlobalParam.com2Fault == 0 && GlobalParam.com3Fault == 0) {
            decimalNumber = Integer.parseInt("0000000000000000", 2);
        }
        return (short) decimalNumber;
    }

    //判断Ark通讯中断
    public static void CommunicationInterrupt(int slaveId) {
//        String bcNum = UtilFile.getFileItem("config.ini", "bcNum");
//        int num = Integer.parseInt(bcNum);
//        for (int i = 0; i < num; i++) {
//            Communication.slaveId[i];
//        }
        switch (slaveId) {
            case 16:
                Communication.ark1_b += 1;
                if (Communication.ark1_b > 3) {
                    Communication.ark1_interrupt = 1;
                    Communication.ark1_a = 0;
                    Communication.ark1_b = 0;
                }
                break;
            case 17:
                Communication.ark2_b += 1;
                if (Communication.ark2_b > 3) {
                    Communication.ark2_interrupt = 1;
                    Communication.ark2_a = 0;
                    Communication.ark2_b = 0;
                }
                break;
            case 18:
                Communication.ark3_b += 1;
                if (Communication.ark3_b > 3) {
                    Communication.ark3_interrupt = 1;
                    Communication.ark3_a = 0;
                    Communication.ark3_b = 0;
                }
                break;
            case 19:
                Communication.ark4_b += 1;
                if (Communication.ark4_b > 3) {
                    Communication.ark4_interrupt = 1;
                    Communication.ark4_a = 0;
                    Communication.ark4_b = 0;
                }
                break;
            case 20:
                Communication.ark5_b += 1;
                if (Communication.ark5_b > 3) {
                    Communication.ark5_interrupt = 1;
                    Communication.ark5_a = 0;
                    Communication.ark5_b = 0;
                }
                break;
            case 21:
                Communication.ark6_b += 1;
                if (Communication.ark6_b > 3) {
                    Communication.ark6_interrupt = 1;
                    Communication.ark6_a = 0;
                    Communication.ark6_b = 0;
                }
                break;
            case 22:
                Communication.ark7_b += 1;
                if (Communication.ark7_b > 3) {
                    Communication.ark7_interrupt = 1;
                    Communication.ark7_a = 0;
                    Communication.ark7_b = 0;
                }
                break;
            case 23:
                Communication.ark8_b += 1;
                if (Communication.ark8_b > 3) {
                    Communication.ark8_interrupt = 1;
                    Communication.ark8_a = 0;
                    Communication.ark8_b = 0;
                }
                break;
            case 2:
                Communication.ammeter_b += 1;
                if (Communication.ammeter_b > 3) {
                    Communication.ammeter_interrupt = 1;
                    Communication.ammeter_a = 0;
                    Communication.ammeter_b = 0;
                }
                break;
            case 1:
                Communication.pvm_b += 1;
                if (Communication.pvm_b > 3) {
                    Communication.pvm_interrupt = 1;
                    Communication.pvm_a = 0;
                    Communication.pvm_b = 0;
                }
                break;

        }


    }

    public static void CommunicationReserve(int slaveId) {
        switch (slaveId) {
            case 16:
                Communication.ark1_a += 1;
                if (Communication.ark1_a > 3) {
                    Communication.ark1_b = 0;
                    Communication.ark1_interrupt = 0;
                    Communication.ark1_a = 0;
                }
                break;
            case 17:
                Communication.ark2_a += 1;
                if (Communication.ark2_a > 3) {
                    Communication.ark2_b = 0;
                    Communication.ark2_interrupt = 0;
                    Communication.ark2_a = 0;
                }
                break;
            case 18:
                Communication.ark3_a += 1;
                if (Communication.ark3_a > 3) {
                    Communication.ark3_b = 0;
                    Communication.ark3_interrupt = 0;
                    Communication.ark3_a = 0;
                }
                break;
            case 19:
                Communication.ark4_a += 1;
                if (Communication.ark4_a > 3) {
                    Communication.ark4_b = 0;
                    Communication.ark4_interrupt = 0;
                    Communication.ark4_a = 0;
                }
                break;
            case 20:
                Communication.ark5_a += 1;
                if (Communication.ark5_a > 3) {
                    Communication.ark5_b = 0;
                    Communication.ark5_interrupt = 0;
                    Communication.ark5_a = 0;
                }
                break;
            case 21:
                Communication.ark6_a += 1;
                if (Communication.ark6_a > 3) {
                    Communication.ark6_b = 0;
                    Communication.ark6_interrupt = 0;
                    Communication.ark6_a = 0;
                }
                break;
            case 22:
                Communication.ark7_a += 1;
                if (Communication.ark7_a > 3) {
                    Communication.ark7_b = 0;
                    Communication.ark7_interrupt = 0;
                    Communication.ark7_a = 0;
                }
                break;
            case 23:
                Communication.ark8_a += 1;
                if (Communication.ark8_a > 3) {
                    Communication.ark8_b = 0;
                    Communication.ark8_interrupt = 0;
                    Communication.ark8_a = 0;
                }
                break;
            case 2:
                Communication.ammeter_a += 1;
                if (Communication.ammeter_a > 3) {
                    Communication.ammeter_b = 0;
                    Communication.ammeter_interrupt = 0;
                    Communication.ammeter_a = 0;
                }
                break;
            case 1:
                Communication.pvm_a += 1;
                if (Communication.pvm_a > 3) {
                    Communication.pvm_b = 0;
                    Communication.pvm_interrupt = 0;
                    Communication.pvm_a = 0;
                }
                break;
        }
    }

    public static short Sys_Alarm_1() {
        short[] Sys_Alarm_1 = new short[]{
                0, 0, 0, 0, 0, 0, 0,
                Communication.ark8_interrupt,
                Communication.ark7_interrupt,
                Communication.ark6_interrupt,
                Communication.ark5_interrupt,
                Communication.ark4_interrupt,
                Communication.ark3_interrupt,
                Communication.ark2_interrupt,
                Communication.ark1_interrupt
        };
        String CommunicationInfo = StringUtils.generateBinaryString(Sys_Alarm_1);
        int info = Integer.parseInt(CommunicationInfo, 2);
        return (short) info;
    }

    public static short Sys_Fault_0() {
        short[] Sys_Fault_0 = new short[]{
                0, Communication.pvm_interrupt, 0, 0, 0, Communication.ammeter_interrupt, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        };
        String Sys_Fault_0Info = StringUtils.generateBinaryString(Sys_Fault_0);
        int info = Integer.parseInt(Sys_Fault_0Info, 2);
        return (short) info;
    }
}
